package errors

// ErrorMessage 错误消息类型
type ErrorMessage string

// 通用错误常量
const (
	// 请求相关错误
	ErrInvalidRequest    ErrorMessage = "请求参数无效"
	ErrInvalidJSON       ErrorMessage = "JSON格式错误"
	ErrMissingParameter  ErrorMessage = "缺少必要参数"
	ErrInvalidParameter  ErrorMessage = "参数格式错误"
	ErrParameterTooLong  ErrorMessage = "参数长度超出限制"
	ErrParameterTooShort ErrorMessage = "参数长度不足"

	// 验证相关错误
	ErrValidationFailed ErrorMessage = "参数验证失败"
	ErrInvalidEmail     ErrorMessage = "邮箱格式错误"
	ErrInvalidPhone     ErrorMessage = "手机号格式错误"
	ErrInvalidPassword  ErrorMessage = "密码格式错误"
	ErrPasswordTooShort ErrorMessage = "密码长度不足"
	ErrPasswordTooWeak  ErrorMessage = "密码强度不够"

	// 认证相关错误
	ErrUnauthorized       ErrorMessage = "未授权访问"
	ErrInvalidToken       ErrorMessage = "无效的访问令牌"
	ErrTokenExpired       ErrorMessage = "访问令牌已过期"
	ErrInvalidCredentials ErrorMessage = "用户名或密码错误"
	ErrAccountLocked      ErrorMessage = "账户已被锁定"
	ErrAccountDisabled    ErrorMessage = "账户已被禁用"

	// 权限相关错误
	ErrForbidden         ErrorMessage = "权限不足"
	ErrAccessDenied      ErrorMessage = "拒绝访问"
	ErrInsufficientScope ErrorMessage = "权限范围不足"

	// 资源相关错误
	ErrNotFound         ErrorMessage = "资源不存在"
	ErrResourceExists   ErrorMessage = "资源已存在"
	ErrResourceConflict ErrorMessage = "资源冲突"
	ErrResourceLocked   ErrorMessage = "资源已被锁定"

	// 业务相关错误
	ErrEmailExists ErrorMessage = "邮箱已被注册"
	ErrPhoneExists ErrorMessage = "手机号已被注册"

	// 系统相关错误
	ErrInternalServer     ErrorMessage = "内部服务器错误"
	ErrServiceUnavailable ErrorMessage = "服务暂时不可用"
	ErrTimeout            ErrorMessage = "请求超时"
	ErrTooManyRequests    ErrorMessage = "请求过于频繁"
	ErrDatabaseError      ErrorMessage = "数据库操作失败"
	ErrCacheError         ErrorMessage = "缓存操作失败"

	// 文件相关错误
	ErrFileNotFound    ErrorMessage = "文件不存在"
	ErrFileTooBig      ErrorMessage = "文件大小超出限制"
	ErrInvalidFileType ErrorMessage = "不支持的文件类型"
	ErrUploadFailed    ErrorMessage = "文件上传失败"
)

// String 实现字符串接口
func (e ErrorMessage) String() string {
	return string(e)
}

// 错误消息映射表（支持国际化）
var ErrorMessages = map[ErrorMessage]map[string]string{
	ErrInvalidRequest: {
		"zh": "请求参数无效",
		"en": "Invalid request parameters",
	},
	ErrInvalidJSON: {
		"zh": "JSON格式错误",
		"en": "Invalid JSON format",
	},
	ErrValidationFailed: {
		"zh": "参数验证失败",
		"en": "Validation failed",
	},
	ErrInvalidEmail: {
		"zh": "邮箱格式错误",
		"en": "Invalid email format",
	},
	ErrInvalidPhone: {
		"zh": "手机号格式错误",
		"en": "Invalid phone number format",
	},
	ErrUnauthorized: {
		"zh": "未授权访问",
		"en": "Unauthorized access",
	},
	ErrTokenExpired: {
		"zh": "访问令牌已过期",
		"en": "Access token has expired",
	},
	ErrInvalidCredentials: {
		"zh": "用户名或密码错误",
		"en": "Invalid username or password",
	},
	ErrForbidden: {
		"zh": "权限不足",
		"en": "Insufficient permissions",
	},
	ErrNotFound: {
		"zh": "资源不存在",
		"en": "Resource not found",
	},
	ErrEmailExists: {
		"zh": "邮箱已被注册",
		"en": "Email already registered",
	},
	ErrInternalServer: {
		"zh": "内部服务器错误",
		"en": "Internal server error",
	},
	ErrTooManyRequests: {
		"zh": "请求过于频繁",
		"en": "Too many requests",
	},
}

// GetLocalizedMessage 获取本地化错误消息
func GetLocalizedMessage(errMsg ErrorMessage, locale string) string {
	if messages, exists := ErrorMessages[errMsg]; exists {
		if localizedMsg, exists := messages[locale]; exists {
			return localizedMsg
		}
		// 回退到中文
		if zhMsg, exists := messages["zh"]; exists {
			return zhMsg
		}
	}
	// 最后回退到常量值
	return string(errMsg)
}

// NewBusinessErrorWithMessage 创建业务错误（带常量）
func NewBusinessErrorWithMessage(errMsg ErrorMessage, code ErrorCode) *AppError {
	return &AppError{
		Code:    code,
		Message: string(errMsg),
	}
}

// NewValidationErrorWithMessage 创建验证错误（带常量）
func NewValidationErrorWithMessage(errMsg ErrorMessage) *AppError {
	return &AppError{
		Code:    ValidationError,
		Message: string(errMsg),
	}
}

// NewAuthErrorWithMessage 创建认证错误（带常量）
func NewAuthErrorWithMessage(errMsg ErrorMessage) *AppError {
	return &AppError{
		Code:    UnauthorizedError,
		Message: string(errMsg),
	}
}
